package com.cskaoyan.gateway.controller.user;

import com.cskaoyan.gateway.form.user.LoginForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.ILoginService;
import com.mall.user.IUserService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.mall.user.intercepter.TokenIntercepter.ACCESS_TOKEN;


/**
 * @author: lds
 * @clsss: LoginController
 */
@Slf4j
@RestController
@RequestMapping("user")
@Transactional(rollbackFor = Exception.class)
public class LoginController {

    @Reference(timeout = 3000, check = false, retries = 0)
    IUserService userService;
    @Reference(timeout = 3000, check = false, retries = 0)
    ILoginService iLoginService;


    @Anoymous
    @GetMapping("login")
    public ResponseData userLogin(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("user/login");
        String token = CookieUtil.getCookieValue(request, ACCESS_TOKEN);

        System.out.println(1);
        if (token == null) {
            return new ResponseUtil().setErrorMsg("");
        }else {
            CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
            checkAuthRequest.setToken(token);

            UserLoginResponse2 userLoginResponse2 = iLoginService.selectUser(checkAuthRequest);
            if (SysRetCodeConstants.SUCCESS.getCode().equals(userLoginResponse2.getCode())) {
                return new ResponseUtil<>().setData(userLoginResponse2.getUserDto());
            }
            return new ResponseUtil().setErrorMsg(userLoginResponse2.getMsg());
        }

    }


    @Anoymous
    @RequestMapping("verify")
    public ResponseData userVerify(String uid, String username) {
        log.debug(uid + "");
        log.debug(username);
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(username);
        userVerifyRequest.setUuid(uid);
        UserVerifyResponse userVerifyResponse = iLoginService.userVerify(userVerifyRequest);
        if (SysRetCodeConstants.SUCCESS.getMessage().equals(userVerifyResponse.getMsg())) {
            return new ResponseUtil<>().setData(null);
        } else if (SysRetCodeConstants.USER_ISVERFIED_ERROR2.getMessage().equals(userVerifyResponse.getMsg())) {
            return new ResponseUtil().setErrorMsg(userVerifyResponse.getMsg());
        } else {
            return new ResponseUtil().setErrorMsg(userVerifyResponse.getMsg());
        }


    }

    @Anoymous
    @PostMapping("login")
    public ResponseData userLoginPost(@RequestBody LoginForm loginForm, HttpServletRequest httpServletRequest,
                                      HttpServletResponse httpServletResponse) {
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");

        UserLoginRequest request = new UserLoginRequest();
        request.setUserName(loginForm.getUserName());
        request.setPassword(loginForm.getUserPwd());
        request.setCaptcha(loginForm.getCaptcha());

        UserLoginResponse response = userService.userLogin(request, uuid);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) {
            Cookie cookie = new Cookie("access_token", response.getToken());
            cookie.setDomain("localhost");
            cookie.setMaxAge(60*30);
            cookie.setPath("/");
            httpServletResponse.addCookie(cookie);

            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }


}
